package com.giraffe.device.config;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @Author ldx
 * @Date 2024/01/26 14:03
 **/
@Data
@Configuration
@Component
public class InfluxDBConfig {

    @Value("${spring.influxdb.url}")
    private String url;

    @Value("${spring.influxdb.token}")
    private String token;

    @Value("${spring.influxdb.bucket}")
    private String bucket;

    @Value("${spring.influxdb.org}")
    private String org;

    @Bean
    public InfluxDBClient influxDBClient() {
        return InfluxDBClientFactory.create(url, token.toCharArray());
    }


   /* public static void main(String[] args) {
        String token = "wXteUXiMoAcdvkeCxxKtlVD6IXUJAw1sspmI3iCBo6b6_-O9ZYP4eQkOfSag1TQR9gvudkZ1Vg_8_1Vel-Qj8g==";
        String bucket = "giraffe_device";
        String org = "giraffe";

        InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://124.223.102.40:8086", token.toCharArray());
        //
        // Write data
        //
        WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();

        //
        // Write by Data Point
        //
        Point point = Point
                .measurement("mem")
                .addTag("host", "host1")
                .addTag("device", "33132131")
                .addField("used_percent", 23.43234543)
                .addField("used_msg", "dsdasda")
                .time(Instant.now(), WritePrecision.NS);
        writeApi.writePoint(bucket, org, point);

        DeleteApi deleteApi = influxDBClient.getDeleteApi();

        //
        // Write by LineProtocol
        //
       // writeApi.writeRecord(WritePrecision.NS, "temperature,location=north value=60.0");

        //
        // Write by POJO
        //
        *//*Temperature temperature = new Temperature();
        temperature.location = "south";
        temperature.value = 62D;
        temperature.time = Instant.now();

        writeApi.writeMeasurement( WritePrecision.NS, temperature);*//*

        //
        // Query data
        //
        *//*String flux = "from(bucket:\"my-bucket\") |> range(start: 0)";

        QueryApi queryApi = influxDBClient.getQueryApi();

        List<FluxTable> tables = queryApi.query(flux);
        for (FluxTable fluxTable : tables) {
            List<FluxRecord> records = fluxTable.getRecords();
            for (FluxRecord fluxRecord : records) {
                System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
            }
        }*//*

        influxDBClient.close();
    }*/
}
